package com.lynn.salary.service.impl;

import java.util.List;
import java.util.Map;

import com.lynn.salary.entity.Article;
import com.lynn.salary.service.IArticleService;

public class ArticleService extends BaseService implements IArticleService {

	public Article save(Article article) throws Exception {

		return dao.save(article);
	}

	public List<Article> findAll() throws Exception {
		return dao.getList(Article.class, "isdeleted<>-1 order by createtime desc");
	}

	public Article getById(String id) throws Exception {
		
		return dao.getById(Article.class, id);
	}
	public List<Map<String, Object>> findByArticleType(String articleType) throws Exception {
		return dao.getPartList(Article.class, new String[]{"title","id","type","createTime"}, "isdeleted=0 and type='"+articleType+"' and tag like '%"+Article.PUBLISH+"%' order by createtime desc limit 0,10");
	}

	public List<Map<String, Object>> findPartArticleByType(String articleType,
			int perpageLength, Integer currentPage) throws Exception {
		return dao.getPartList(Article.class, new String[]{"title","id","type","createTime"}, "isdeleted=0 and type='"+articleType+"' and tag like '%"+Article.PUBLISH+"%' order by createtime desc limit " + (currentPage - 1)*perpageLength + "," +  perpageLength);
	}
	
	public long findAllArticleNumber(String articleType) throws Exception{
		List<Map<String, Object>> list =  dao.getPartList(Article.class, new String[]{"count(id) as total"}, "isdeleted=0 and type='"+articleType+"' and tag like '%"+Article.PUBLISH+"%' order by createtime desc");
		
		return (Long)list.get(0).get("total");
	}

//	public boolean delete(String[] ids) throws Exception {
//		try{
//			StringBuffer sql = new StringBuffer("update "+Article.getTableName() +" set isdeleted=-1 where id in (");
//			int i = 0;
//			for(String id : ids) {
//				if(i++ != 0) {
//					sql.append(",");
//				}
//				sql.append("'");
//				sql.append(id);
//				sql.append("'");
//			}
//			sql.append(")");
//			
//			dao.executeUpdateSQL(sql.toString());
//		}catch(Exception e) {
//			e.printStackTrace();
//			return false;
//		}
//		return true;
//	}
	

}
